package org.android.brasil.projetos.oktoberfest2011.view.controller;

import org.android.brasil.projetos.oktoberfest2011.R;
import org.android.brasil.projetos.oktoberfest2011.utils.ControleDeBackground;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * Cria um mapa em um webview, dentro do mapa existem pontos que possuem animacoes
 * 
 * @author Henrique Deodato
 * 
 */
public class MapaController extends Activity implements OnClickListener {

	private static final int ABOUT = Menu.FIRST;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.tabs_mapa);
		
		//get the webview element
		WebView webView = (WebView) findViewById(R.id.webview);
		
		//enable the javascript 
		webView.getSettings().setJavaScriptEnabled(true);
		
		//enable zoom support and zoom controls
		webView.getSettings().setSupportZoom(true);
		webView.getSettings().setBuiltInZoomControls(true);
		webView.getSettings().setUseWideViewPort(true);
		webView.setInitialScale(9);
		
		//remove the scrollbars
		webView.setHorizontalScrollBarEnabled(false);
		webView.setVerticalScrollBarEnabled(false);
		
		//point to our mapa.html file
		webView.loadUrl("file:///android_asset/mapa.html");
		
		// ponte entre javascript e java
		webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
		
		//define our "browser"
		webView.setWebViewClient(new MapaWebClient());
		
		//get the button and set the click to home...
		ImageView casa = (ImageView) findViewById(R.id.casa);
		casa.setOnClickListener(this);
		
		RelativeLayout rl = (RelativeLayout) findViewById(R.idMapas.relativeLayout01);
		ControleDeBackground.SetBarraSuperiorBackGround(getApplicationContext(),rl);

	}
	
	public void onClick(View v) {
	      switch(v.getId()){
	      	case R.id.casa:
	      		setResult(RESULT_OK);
				finish();
	    	  break;
	    	  
	    	default:
	    		
	    		break;
	      }
	    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onResume() {
		super.onResume();

	}
	
	// -------Create the menu-------
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		menu.add(0, ABOUT, 0, R.string.btn_about).setIcon(R.drawable.ic_info);

		return super.onCreateOptionsMenu(menu);
	}

	// handle the menu buttons
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i = null;

		switch (item.getItemId()) {

		case ABOUT:
			i = new Intent(this, SobreController.class);
			startActivity(i);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	// ------- end of menu creation --------

	class MapaWebClient extends WebViewClient {
	    @Override
	    public boolean shouldOverrideUrlLoading(WebView view, String url) {
	        view.loadUrl(url);
	        return true;
	    }
	}
	
	// classe que envia dados do java para o javascript
	class JavaScriptInterface {
		Context mContext;
		
		// variaveis da tela
		int height;
		int width;
		int orientation;
		float dpi;

		/** Instantiate the interface and set the context */
		JavaScriptInterface(Context c) {
			mContext = c;
			
			// pega as infos sobre a tela
			height = c.getResources().getDisplayMetrics().heightPixels;
			width = c.getResources().getDisplayMetrics().widthPixels;
			orientation = c.getResources().getConfiguration().orientation;
			dpi = c.getResources().getDisplayMetrics().density;
		}

		/** Show a toast from the web page */
		public void showToast(String toast) {
			Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
		}
		/**
		 * Retorna a densidade da tela
		 * */
		public float getDpi() {
			return dpi;
		}


		/**
		 * Return the translated string*/
		public String getTransString(String str){
			String ret;
			ret = getString(getResources().getIdentifier("org.android.brasil.projetos.oktoberfest2011:string/"+str, null, null));
			if(ret == null){
				ret = "";
			}
			return ret;
		}
	}

}